﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace Photo_Manipulator
{
    public partial class frmMain : Form
    {
        #region Constants
        const int THUMB_WIDTH = 48;
        const int THUMB_HEIGTH = 48;
        #endregion

        #region Private variables
        int choosen = 0;
        private Color _Color1 = Color.Gainsboro;
        private Color _Color2 = Color.White;
        private float _ColorAngle = 30f;
        private ImageList imgListSrc, imgListDest;
        private string srcPath, destPath;
        #endregion

        #region Public properties
        public Color Color1
        {
            get { return _Color1; }
            set
            {
                _Color1 = value;
                this.Invalidate(); // Tell the Form to repaint itself
            }
        }

        public Color Color2
        {
            get { return _Color2; }
            set
            {
                _Color2 = value;
                this.Invalidate(); // Tell the Form to repaint itself
            }
        }

        public float ColorAngle
        {
            get { return _ColorAngle; }
            set
            {
                _ColorAngle = value;
                this.Invalidate(); // Tell the Form to repaint itself
            }
        }
        #endregion

        #region On paint events
        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            // Getting the graphics object
            Graphics g = pevent.Graphics;

            // Creating the rectangle for the gradient
            Rectangle rBackground = new Rectangle(0, 0,
                                      this.Width, this.Height);

            // Creating the lineargradient
            System.Drawing.Drawing2D.LinearGradientBrush bBackground
                = new System.Drawing.Drawing2D.LinearGradientBrush(rBackground,
                                                  _Color1, _Color2, _ColorAngle);

            // Draw the gradient onto the form
            g.FillRectangle(bBackground, rBackground);

            // Disposing of the resources held by the brush
            bBackground.Dispose();
        }
        #endregion

        public frmMain()
        {
            
            this.Color1 = System.Drawing.Color.Gainsboro;
            this.Color2 = System.Drawing.Color.LightSteelBlue;
            this.ColorAngle = 45.0f;
            // Makes sure the form repaints when it was resized
            this.SetStyle(ControlStyles.ResizeRedraw, true);

            

            InitializeComponent();
            // Add forms at runtime 
            // each tab that is needed is a separate form
            // Add them dynamically to each tab page.


            dgvHorseShowFetch.Visible = true;
            lstSourceThumb.Visible = false;
            lblSourceThumb.Visible = false;
            picBoxHorse.Visible = false;

            //Tabpage 1
            URLInfoConfig URLConfig = new URLInfoConfig();
            URLConfig.TopLevel = false;
            tabForms.TabPages[0].Controls.Add(URLConfig);
            tabForms.TabPages[0].Text = "Horse Show URL";
            URLConfig.Show();


            // Tabpage 2
            Locations loc = new Locations(this);
            loc.TopLevel = false;
            tabForms.TabPages[1].Controls.Add(loc);
            tabForms.TabPages[1].Text = "Locations";
            loc.Show();


            //Tab page 3
            UserDataEntry userDataEntry = new UserDataEntry();
            userDataEntry.TopLevel = false;
            tabForms.TabPages[2].Controls.Add(userDataEntry);
            tabForms.TabPages[2].Text = "User Data";
            userDataEntry.Show();

            //Tab page 4

            GenericDataForm genericDataForm = new GenericDataForm();
            genericDataForm.TopLevel = false;
            tabForms.TabPages[3].Controls.Add(genericDataForm);
            tabForms.TabPages[3].Text = "Non-Horse Show Events";
            genericDataForm.Show();

            // Tab page 5
            Options opt = new Options();
            opt.TopLevel = false;
            tabForms.TabPages[4].Controls.Add(opt);
            tabForms.TabPages[4].Text = "Options";
            opt.Show();


            imgListDest = new ImageList();
            imgListSrc = new ImageList();
            imgListDest.ImageSize = new Size(THUMB_WIDTH, THUMB_HEIGTH);
            imgListSrc.ImageSize = new Size(THUMB_WIDTH, THUMB_HEIGTH);
            imgListDest.ColorDepth = ColorDepth.Depth24Bit;
            imgListSrc.ColorDepth = ColorDepth.Depth24Bit;
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void btnQuit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void AddSourceThumbnails(string folderPath)
        {
            srcPath = folderPath;
            Thread th = new Thread(delegate() {
                AddThumbnailstoList(folderPath, lstSourceThumb, imgListSrc);});
            th.Start();
        }

        delegate void AddItemtoListDelegate(ListView refLV, string s, int c);
        delegate void ClearListDelegate(ListView refLV, ImageList imglist);
        delegate void AddThumbtoImgListDelegate(ImageList imgList, Image img);

        public void AddItemToList(ListView refLV, string s, int c)
        {
            refLV.Items.Add(s);
            refLV.Items[c].ImageIndex = c;
            refLV.View = View.LargeIcon;
        }

        public void AddThumbToImgList(ImageList imgList, Image img)
        {
            imgList.Images.Add(img);
        }

        public void ClearList(ListView refLV, ImageList imglist)
        {
            refLV.Clear();
            imglist.Images.Clear();
            refLV.LargeImageList = imglist;
            
        }

        private void AddThumbnailstoList(string folderPath, ListView refLV, ImageList imgList)
        {
            try
            {
                string[] filePaths = Directory.GetFiles(folderPath, "*.jpg");
                int count = 0;
                object[] obj = new object[2];
                obj[0] = refLV;
                obj[1] = imgList;
                refLV.BeginInvoke(new ClearListDelegate(ClearList), obj);
                
                foreach (string s in filePaths)
                {
                    Image img = System.Drawing.Image.FromFile(s);
                    Image thumb = img.GetThumbnailImage(THUMB_WIDTH, THUMB_HEIGTH, null, IntPtr.Zero);
                    String[] splitStr = s.Split(new char[] { '\\' });
                    obj = new object[2];
                    obj[0] = imgList;
                    obj[1] = thumb;
                    refLV.BeginInvoke(new AddThumbtoImgListDelegate(AddThumbToImgList), obj);
                    obj = new object[3];
                    obj[0] = refLV;
                    obj[1] = splitStr[splitStr.Length - 1];
                    obj[2] = count;
                    refLV.BeginInvoke(new AddItemtoListDelegate(AddItemToList), obj);
                    count++;
                    img.Dispose();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Source + ":" + ex.Message);
            }

        }

        private void lstSourceThumb_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstSourceThumb.SelectedItems.Count > 0)
            {
                string filePath = srcPath + "\\" + lstSourceThumb.SelectedItems[0].Text;
                ImagePreview img = new ImagePreview(filePath);
                img.TopMost = true;
                img.Show();
                
            }
        }

        private void tabForms_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabForms.SelectedIndex == 0)
            {
                lstSourceThumb.Visible = false;
                lblSourceThumb.Visible = false;
                dgvHorseShowFetch.Visible = true;
                picBoxHorse.Visible = false;
            }
            else if (tabForms.SelectedIndex == 1)
            {
                dgvHorseShowFetch.Visible = false;
                lstSourceThumb.Visible = true;
                lblSourceThumb.Visible = true;
                picBoxHorse.Visible = false;
            }
            else if (tabForms.SelectedIndex== 2)
            {
                lstSourceThumb.Visible = true;
                lblSourceThumb.Visible = true;
                dgvHorseShowFetch.Visible = false;
                lstSourceThumb.Height = 424;
                lstSourceThumb.Width = 394;
                picBoxHorse.Visible = false;
            }
            else if (tabForms.SelectedIndex == 3)
            {
                lstSourceThumb.Visible = true;
                lblSourceThumb.Visible = true;
                dgvHorseShowFetch.Visible = false;
                picBoxHorse.Visible = false;
            }
            else if (tabForms.SelectedIndex == 4)
            {
                lstSourceThumb.Visible = false;
                lblSourceThumb.Visible = false;
                dgvHorseShowFetch.Visible = false;
                picBoxHorse.Visible = true;
            }

        }

        private void tabForms_TabIndexChanged(object sender, EventArgs e)
        {
            

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void aboutUsToolStripMenuItem_Click(object sender, EventArgs e)
        {
           MessageBox.Show("Photo Manipulator and Custom Image Manager. \r\n Version 1.0. Prototype-1.\r\n\n Made by Innovation For Captured Moment Photography", "About", MessageBoxButtons.OK);
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            Locations location = new Locations(this);
           
            if (choosen == 0)
            {

            }
        

            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("c:\\Sri\\Test");
            string path = @"C:\Sri\AppTestCSharp";
            System.IO.Directory.CreateDirectory(path);
            
            if (System.IO.Directory.Exists(path))
            {
                DialogResult overwriteOrNot = MessageBox.Show("Do you want to overwrite the existing file?", "Warning!", MessageBoxButtons.YesNoCancel);
                if (overwriteOrNot == DialogResult.Yes)
                {
                    //dir.GetFiles().
                }

                else if (overwriteOrNot == DialogResult.No)
                {

                }
                else if (overwriteOrNot == DialogResult.Cancel)
                {

                }
            }
          //  for (int i = 0; i < dir.GetFiles().Length; i++)
            //{
                
            //}
            
        }

     /*   private dataValuesFromMain dgvHorseShowFetch_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
           dataValuesFromMain dgv = new dataValuesFromMain(;
            for (int i = 1; i < dgvHorseShowFetch.RowCount-1; i++)
            {
                dgv = new dataValuesFromMain((int)dgvHorseShowFetch.Rows[i].Cells[0].Value,dgvHorseShowFetch.Rows[i].Cells[1].Value.ToString(), dgvHorseShowFetch.Rows[i].Cells[2].Value.ToString());
            }
            return dgv;

        }*/

      

     /*   private void locObjectects()
        {
            Locations loc = new Locations(this);
            

        }*/


    }
}
